var liaos = [];
var isUpate = false;//是否是修改，默认不是
var currentRow;
var search_data = {page:1,limit:10,
    wuliaomingcheng:stitle.value,
    wuliaobianma:stitle.value};
loadData();

function loadData(data){
    list_div.innerHTML = "";
    loading.style.display = "block";
    ajax("/find_all_liao",search_data,function(data){
        liaos = data;
        showData( liaos);
        loading.style.display = "none";
    });
    
    ajax("/count_all_liao",search_data,function(data){
        // console.log(data);  33
        data = parseInt(data);
        current_page.innerHTML = search_data.page;
        total_page.innerHTML =
            (data%search_data.limit==0?
                data/search_data.limit:
                Math.floor(data/search_data.limit)+1);
    });
}
function pre_page(){
    if(search_data.page>1) {
        search_data.page--;
        loadData();
    }
}
function next_page(){
    if(search_data.page<parseInt(total_page.innerHTML)) {
        search_data.page++;
        loadData();
    }
}
function first_page(){

    search_data.page=1;
    loadData();

}
function last_page(){

    search_data.page=parseInt(total_page.innerHTML);
    loadData();

}
function to_page(){
    var ip = parseInt(input_page.value);
    if(ip>=1&&ip<=parseInt(total_page.innerHTML)){
        search_data.page = ip;
        loadData();
    }
}


function showData(arr){
    list_div.innerHTML = "";
    if(arr.length==0){
        list_div.innerHTML = "暂无数据";
        return;
    }
    var allhtml = "";
    allhtml += "<table cellpadding='0' cellspacing='0'" +
        " width='100%'><tr class='theader'>" +
        "<th>物料类型</th><th>物料名称</th><th>物料编码</th><th>物料SN</th>" +
        "<th>供应商类型</th><th>供应商名称</th><th>供应商编码</th><th>物料仓库</th><th>成本价</th><th>状态</th><th>操作</th></tr>";
    for(var i=0;i<liaos.length;i++){
        var a = arr[i];

        allhtml +=
            "<tr class='list'>" +
            "<td class='ntitle'>"+a.wuliaoleixing+"</td>" +
            "<td class='ntitle'>"+a.wuliaomingcheng+"</td>" +
            "<td class='ntitle'>"+a.wuliaobianma+"</td>" +
            "<td class='ntitle'>"+a.wuliaoSN+"</td>" +
            "<td class='ntitle'>"+a.gongyingshangleixing+"</td>" +
            "<td class='ntitle'>"+a.gongyingshangmingcheng+"</td>" +
            "<td class='ntitle'>"+a.gongyingshangbianma+"</td>" +
            "<td class='ntitle'>"+a.wuliaocangku+"</td>" +
            "<td class='ntitle'>"+a.chengbenjia+"</td>" +
            "<td class='ntitle'>"+a.zhuangtai+"</td>" +
            "<td class='ntitle'><a class='edit_a' onclick='upd("+i+")' href='javascript:;'>编辑</a>" +
            "<a class='del_a' onclick='del("+a.id+")' href='javascript:;'>删除</a></td>" +
            "</tr>";
    }
    allhtml += "</table>";
    list_div.innerHTML = allhtml;
}
function search(){
    search_data = {page:1,limit:10,
    wuliaomingcheng:stitle.value,
    wuliaobianma:stitle.value}
    loadData();
}

function save(){
    if(!wuliaomingcheng.value | !wuliaobianma.value){
        wuliaomingcheng_error.innerHTML = "物料名称不能为空";

        wuliaobianma_error.innerHTML = "物料编码不能为空";
        return;
    }
    
    ajax(isUpate?"/upd_liao":"/add_liao",{
        id:currentRow?currentRow.id:-1,
         wuliaoleixing:wuliaoleixing.value,
         wuliaomingcheng:wuliaomingcheng.value,
         wuliaobianma:wuliaobianma.value,
         wuliaoSN:wuliaoSN.value,
         gongyingshangleixing:gongyingshangleixing.value,
         gongyingshangmingcheng:gongyingshangmingcheng.value,
         gongyingshangbianma:gongyingshangbianma.value,
         wuliaocangku:wuliaocangku.value,
         chengbenjia:chengbenjia.value,
         zhuangtai:zhuangtai.value
     },function(data){
         alert(isUpate?"更新成功！":"新增成功！");
         wuliaoleixing.value = "";
         wuliaomingcheng.value = "";
         wuliaobianma.value = "";
         wuliaoSN.value = "";
         gongyingshangleixing.value = "";
         gongyingshangmingcheng.value = "";
         gongyingshangbianma.value = "";
         wuliaocangku.value = "";
         chengbenjia.value = "";
         zhuangtai.value = "";
         loadData();
         add_div.style.display = "none";
         list_div.style.display = "block";
         save_btn.style.display = "none";
         add_btn.style.display = "block";
         search_div.style.display = "block";
         return_btn.style.display = "none";
     })
    }
     function del(id){
        if(!confirm("您确定要删除这个数据吗？")){
            return;
        }
        ajax("/del_liao", {id:id},function(data){
            alert("删除成功！");
            loadData();
        });
    }
    
function upd(i){
    isUpate = true;
    var d = liaos[i];
    currentRow = d;
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";

        wuliaoleixing.value=d.wuliaoleixing;
        wuliaomingcheng.value=d.wuliaoleixing;
        wuliaobianma.value=d.wuliaobianma;
        wuliaoSN.value=d.wuliaoSN;
        gongyingshangleixing.value=d.gongyingshangleixing;
        gongyingshangmingcheng.value=d.gongyingshangmingcheng;
        gongyingshangbianma.value=d.gongyingshangbianma;
        wuliaocangku.value=d.wuliaocangku;
        chengbenjia.value=d.chengbenjia;
        zhuangtai.value=d.zhuangtai;
        wuliaomingcheng.focus();
}

function add(){
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";

        wuliaoleixing.value = "";
         wuliaomingcheng.value = "";
         wuliaobianma.value = "";
         wuliaoSN.value = "";
         gongyingshangleixing.value = "";
         gongyingshangmingcheng.value = "";
         gongyingshangbianma.value = "";
         wuliaocangku.value = "";
         chengbenjia.value = "";
         zhuangtai.value="";

}
function returnMain(){
    add_div.style.display = "none";
    list_div.style.display = "block";
    save_btn.style.display = "none";
    add_btn.style.display = "block";
    search_div.style.display = "block";
    return_btn.style.display = "none";
    wuliaoleixing.value = "";
    wuliaomingcheng.value = "";
    wuliaobianma.value = "";
    wuliaoSN.value = "";
    gongyingshangleixing.value = "";
    gongyingshangmingcheng.value = "";
    gongyingshangbianma.value = "";
    wuliaocangku.value = "";
    chengbenjia.value = "";
    zhuangtai.value="";
  }
